<?php

defined('CITY_BAO') or exit('Access Denied');
require CB_CORE . 'modules/' . $config_module['module'] . '/common.inc.php';
if ($user->userid > 0)
    redirect(INSTALL_PATH . 'member/');
if (isset($config_member['enable_register']) && !$config_member['enable_register']) {
    show_message('对不起，本站暂时不允许新用户注册！', INSTALL_PATH);
}
if (isset($config_member['baned_agent']) && !empty($config_member['baned_agent'])) {
    $baned_agents = explode("|", $config_member['baned_agent']);
    foreach ($baned_agents as $baned_agent) {
        if (strpos($_SERVER['HTTP_USER_AGENT'], $baned_agent) !== false)
            show_message('您的客户端信息已经被网站屏蔽!如有疑问，请与我们联系!', INSTALL_PATH);
    }
}
if (isset($config_member['ip_timeout']) && !empty($config_member['ip_timeout'])) {
    $timeout = TIME_STAMP - $config_member['ip_timeout'] * 3600;
    $temp = $cbdb->get_row("SELECT userid FROM {#cbdbPrefix}members WHERE regip='" . IP_ADDRESS . "' AND regtime>'" . $timeout . "' ");
    if ($temp) {
        show_message('同一IP' . $config_member['ip_timeout'] . '小时内只能注册一次', INSTALL_PATH);
    }
    unset($temp);
}
$forward = $request->getParam('forward');
if (!$forward) {
    $reffer = $request->getReffer();
    $forward = $reffer ? $reffer : SITE_URL;
    if (strpos($forward, 'member/register')) {
        $forward = SITE_URL;
    }
}
$form_validate = form_validate('register');
$tpl->assign('part', 'form');
$captcha_register = isset($config_member['captcha_register']) ? $config_member['captcha_register'] : 1;
if ($request->isPost()) {
    $username = trim($request->getParam('username'));
    $password = trim($request->getParam('password'));
    $password2 = trim($request->getParam('password2'));
    $email = trim($request->getParam('email'));
    $flag = true;
    $validate = trim($request->getParam('validate'));
    if ($validate != $form_validate) {
        $error = '数据来源校验失败！';
        $flag = false;
    }
    if ($flag && $captcha_register) {
        $captcha = trim($request->getParam('captcha'));
        if (!isset($_SESSION['AuthCode'])) {
            $error = '验证码已过期！';
            $flag = false;
        } elseif (md5($captcha) != $_SESSION['AuthCode']) {
            $error = '验证码输入错误！';
            $flag = false;
        }
        unset($_SESSION['AuthCode']);
    }
    if ($flag) {
        $agree = trim($request->getParam('after'));
        if (!$agree) {
            $error = '您必须同意本站服务条款后才能注册为本站会员！';
            $flag = false;
        }
    }
    if ($flag) {
        $user = $cbdb->get_row('SELECT * FROM {#cbdbPrefix}members WHERE username="' . $username . '" LIMIT 0,1');
        if ($user) {
            $error = "用户名{$username}已经存在！<br /> 如果该用户名是您注册的，请使密码找回功能，如果不是您注册的，请您更换用户名后再注册。";
            $flag = false;
        }
        unset($user);
    }
    if ($flag) {
        $user = $cbdb->get_row('SELECT * FROM {#cbdbPrefix}members WHERE email="' . $email . '" LIMIT 0,1');
        if ($user) {
            $error = "Email地址{$email}已经被注册！<br /> 如果该Email是您注册的，请使密码找回功能，如果不是您注册的，请您更换Email后再注册。";
            $flag = false;
        }
        unset($user);
    }
    if ($flag) {
        $error = register_check($request->getParams());
        if ($error === true) {
            $insert_data = array(
                'username' => $username,
                'password' => md5($password),
                'payword' => '',
                'email' => $email,
                'newmessage' => '0',
                'credit' => '0',
                'regip' => IP_ADDRESS,
                'regtime' => TIME_STAMP,
                'keep_loginip' => '',
                'last_loginip' => IP_ADDRESS,
                'last_logintime' => TIME_STAMP,
                'logintimes' => '0'
            );
            if (isset($config_member['register_verify']) && $config_member['register_verify']) {
                $insert_data['groupid'] = 4; //4待审核会员
            } else {
                $insert_data['groupid'] = 3; //3个人会员
            }
            $result = $cbdb->insert('{#cbdbPrefix}members', $insert_data);
            if ($result) {
                $userid = $cbdb->get_last_insert_id();
                $profile = array(
                    'userid' => $userid,
                    'gender' => 0
                );
                $cbdb->insert('{#cbdbPrefix}member_profile', $profile);
                $stats = array(
                    'userid' => $userid
                );
                $cbdb->insert('{#cbdbPrefix}member_stats', $stats);
                if (isset($config_member['log_user_register']) && $config_member['log_user_register']) {
                    $logger->log("{$username}于" . date('Y-m-d H:i:s', TIME_STAMP) . "在" . IP_ADDRESS . "处注册新用户。", 'user_register', $username);
                }
                if (isset($config_member['after_register_login']) && !$config_member['after_register_login']) {
                    $tpl->assign('after_register_login', 0);
                    $user = (object) $insert_data;
                    $user->userid = $cbdb->insert_id;
                    set_cookie('auth_user', $user);
                    if (isset($config_member['log_user_login']) && $config_member['log_user_login']) {
                        $logger->log("{$username}于" . date('Y-m-d H:i:s', TIME_STAMP) . "在" . IP_ADDRESS . "处注册新用户并登录了。", 'user_login', $username);
                    }
                } else {
                    $tpl->assign('after_register_login', 1);
                }
            } else {
                $error = '用户写入数据库失败，如果这个错误是第一次出现，请重试！如果多次出现，请与系统管理员联系。';
                $flag = false;
            }
        } else {
            $flag = false;
        }
    }
    if ($flag) {
        $tpl->assign('title', "用户注册成功");
        $tpl->assign('part', 'success');
        //redirect($forward);
    } else {//注册失败
        $tpl->assign('error', $error);
        $tpl->assign('title', $error);
    }
    $tpl->assign('username', $username);
    $tpl->assign('password', $password);
    $tpl->assign('email', $email);
} else {
    $tpl->assign('title', '用户注册');
}
$tpl->assign('validate', $form_validate);
$tpl->assign('forward', $forward);
$tpl->assign('captcha_register', $captcha_register);
$tpl->display('member/register.tpl');